//app.js
var ip= "www.qqlian.org";
var iptest = "localhost:8080/qql";
// var ip="localhost:8084/qql";
var server_host="https://" + ip + "/";
var server_hosttest = "http://" + iptest + "/";
var base64Util=require('untils/Base64.js');
var promise=require('untils/es6Promise.js');

App({
  onLaunch: function () {
    wx.setStorageSync('addName','浙江省');
    wx.setStorageSync('addCode','33');
  },
  globalData: {
    picServer: "http://" + ip + "/",
    // userid: 7732
    userid: '',//用户id
    http_url:"http://www.qqlian.org/",
    https_url: "https://www.qqlian.org/",
    ypy_url: "http://appapp.b0.upaiyun.com/",
    DICTIONARY: server_host + 'wxDataDictionary',
    COMMON:{
      //根据坐标获得地点名称
      GET_AREA: server_host + 'wxGis',
      ADDRLIST: server_host + 'wxRegion',
      WX_SIGN: server_host + 'wxConfig',
    },
    SERVICE:{
      ACTIVITY:{
        LIST: server_host + 'wxBlindList',
        DETAILS: server_host + 'wxBlindDetail',
        APPLIEDS: server_host + 'wxBlindPartLists',
        APPLY: server_host + 'wxBlindPartSave',
        CANCELAPPLY: server_host + 'wxBlindPartDel',
        BLINDPART: server_host + 'wxBlindPartDetail',
      },
      MATES: {
        VERIFY: server_host + 'WxYzMember',
        LIST: server_host + 'wxMemberList',
        DETAILS: server_host + 'wxMemberDetail',
        ATTENTION: server_host + 'wxConcernSave',
        NOATTENTION: server_host + 'wxConcernSave1',
        GREETEDLIST: server_host + 'wxgreetedList',
        SAYHI: server_host + 'wxConMsgSave',
        ADDR: server_host + 'wxRegion',
        VERIFYHD: server_host + 'WxYzMemberHd',
      },
      INDEX:{
        MYMOOD: server_host + 'wxDynamicList',
        LISTSP: server_host + 'wxMemberSpList',
        ACTIVITYBASEDT: server_host + 'wxBlindBaseListMap',
        ACTIVITYBASE: server_host + 'wxBlindBaseList',
        ACTIVITYBASEDETAIL: server_host + 'wxBlindBaseDetail',
        PHOTOLIST: server_host + 'wxBlindImgList',
        PHOTOLISTDETAIL: server_host + 'wxBlindImgDetail',
        MSGDETAIL: server_host + 'wxMsgDetail',
        MOODMSGSAVE: server_host + 'wxDynamicMsgSave',
        ADD: server_host + 'wxRegions',
        MOODPUBLIC: server_host + 'wxDynamicSave',
        MEMBERDT: server_host + 'wxMemberListMap',
      },
      USERCENTER:{     
        // 绑定手机号
        PHONE_BIND: server_host + 'wxMemberBd',
        	// 注册
        REGISTER: server_host + 'wxMemberSave1',
        MEMBER_DETAIL: server_host + 'wxMemberDetail',
        // 爱情会客厅
        ACCOUNT_ROOM: server_host + 'wxRoomSave',
        // 读取签到信息
        LOAD_CHECKIN: server_host + 'wxSignActivity',
        // 签到
        CHECKIN: server_host + 'wxSignSave',
        // 我的相册
        MYALBUMLIST: server_host + 'wxAlbumList',
        // 设为形象照片
        IMGSAVE: server_host + 'wxAlbumImgSave',
        // 相册删除
        ALBUMDEL: server_host + 'wxAlbumDel',
        // 相册上传
        IMGUPLOAD: server_host + 'annexs',
        // 图片上传服务器
        IMGTOSERVER: server_host + 'wxAlbumSave',
        // 头像上传
        AVATARUPLOAD: server_host + 'annex',
        // 头像上传
        USERHEAD: server_host + 'wxImgSave',
        // 内心独白保存
        MY_INNER_SAVE: server_host + 'wxMemberSave2',
        // 个人标签保存
        TAG_SAVE: server_host + 'wxMemberDetailSave5',
        // 基本信息保存
        BASIC_INFO_JB_SAVE: server_host + 'wxMemberJbSave',
        // 基本信息保存
        BASIC_INFO_XX_SAVE: server_host + 'wxMemberXxSave',
        // 性格外貌修改
        OUTSIDE_SAVE: server_host + 'wxMemberDetailSave1',
        //兴趣爱好
        HOBBY_SAVE: server_host + 'wxMemberDetailSave4',
        // 择偶条件修改
        MATE_CONDITION_SAVE: server_host + 'wxMemberMateSave',
        // 修改密码
        PASSWORD_UPDATE: server_host + 'wxMmSave',
        // 会员关闭
        MEMBER_UPTZ: server_host + 'wxMemberGbSave',
        IMGUPLOADDO: server_host + 'doannex',
        // 用户列表 id,type,pageno,pagesize
        USER_LIST: server_host + 'wxConcernList',
        // 留言&打招呼列表
        GREET_LIST: server_host + 'wxConMsgList',
        // 我的消息列表
        MESSAGE_LIST: server_host + 'wxMessList',
        BLACK_LIST: server_host + 'wxBlackList',
        // 投诉
        COMPLAIN: server_host + 'wxComplaintSave',
        // 发送验证码
        SEND_CAPTCHA: server_host + 'wxCode',
        // 删除心情
        MOOD_DEL: server_host + 'wxDynamicDel'
      },
      INFO: {
        LIST: server_host + 'wxMsgList'
      },
      TIEBA: {
        REPLY: server_host + 'wxThemeAnswerSave',
      },
      AUTH: {
        SAVE: server_host + 'wxMemberSave4'
      },
    }
  },
  getUserInfo: function (data){//获得用户信息
    wx.request({
      url: getApp().globalData.SERVICE.USERCENTER.MEMBER_DETAIL,
      data: data,
      method: "GET",
      dataType: "json",
      success: function (res) {
        if (res.data.code == 200) {
          wx.setStorageSync("userInfoEnctype", base64Util.Base64.encode(encodeURIComponent(JSON.stringify(res.data.obj))));
          wx.setStorageSync("userInfo", res.data.obj);
          getApp().globalData.userid = res.data.obj.member.id
        } else if (res.data.code == 500) {//查询无会员
          wx.navigateTo({
            url: '../../pages/register/register'
          });
        }
      }
    })
  },
  getUserInfoSyn:function(data){//获得用户信息（因为wx.request是异步的，用promise封装一个同步的方法）
    return new Promise(function (resolve, reject){
      wx.request({
        url: getApp().globalData.SERVICE.USERCENTER.MEMBER_DETAIL,
        data: data,
        method: "GET",
        dataType: "json",
        success: function (res) {
          if (res.data.code == 200) {
            console.log("成功获得用户信息")
            wx.setStorageSync("userInfoEnctype", base64Util.Base64.encode(encodeURIComponent(JSON.stringify(res.data.obj))));
            wx.setStorageSync("userInfo", res.data.obj);
            getApp().globalData.userid = res.data.obj.member.id
          }else if (res.data.code==500){//查询无会员
            wx.navigateTo({
              url: '../../pages/register/register'
            });
          }
          resolve(res);
        }
      })
    });
  },
  base64Encode:function(data){
    return base64Util.Base64.encode(data);
  },
  base64Decode:function(data){
    return base64Util.Base64.decode(data);
  },
  confirm:function(title,content){//封装的类似于原生的confirm方法
   return new Promise(function (resolve,reject){//小程序的异步方法success回调无法return值，又因为是强制的异步方法，所以用promise来return值
    wx.showModal({
      title: title,
      content: content,
      success: function (res) {
        if (res.confirm) {
          resolve(true);
        } else if (res.cancel) {
          resolve(false);
        }
      }
    });
  });
  },
  doReq:function(url,data,method,success){
    if(method=='post'){
      wx.request({
        url: url,
        data: data,
        header: { "Content-Type": "application/x-www-form-urlencoded" },
        method: method,
        dataType: "json",
        success: success
      });
    }else{
    wx.request({
      url: url,
      data: data,
      method: method,
      dataType: "json",
      success: success
    });
    }
    
  },
  doReqSyn: function (url, data, method){
    return new Promise(function (resolve, reject){
            wx.request({
              url: url,
              data: data,
              method: method,
              dataType: "json",
              success: function(res){
                resolve(res);
              }
            });
        });
  },
  getBindDataSet:function(event){//获得dataset的数据
    return event.currentTarget.dataset;
  },
ifLogin:function(){
   var _this=this;
  return new Promise(function(reslove,reject){
      var openid = wx.getStorageSync('openid');
      var unionid=wx.getStorageSync('unionid');
      _this.doReq(server_host + '/qql/getUserByOpenid', { openid: openid},'get',function(res){
        let entity =res.data.entity;
        if(!entity){//如果用户不存在，向表里插入数据，并且跳转到注册页面，如果存在去查询用的基本信息并且保存
          _this.doReq(server_host + 'qql/add', {
              uid: unionid,
              openid: openid,
               wxUserInfo: '',
               pl: 2,
               },'post',function(res){
                    reslove();
                    if(res.data){
                      wx.navigateTo({
                      url: '../../pages/register/register'
                    });
                  }
               });
        }else{
              reslove();
              _this.getUserInfo({"openId": wx.getStorageSync('openid')});
        }
      });
  })
},
wxLogin:function(){
  var _this = this;
  return new Promise(function(reslove){
    wx.login({//通过微信接口获得用户的openid
      success: function (res) {
        let code = res.code;
        wx.request({
          url: server_host + '/qql/wxLogin',
          method: "GET",
          dataType: "json",
          data: {
            js_code: code
          },
          success: function (res) {
            let data = res.data.res;
            var opid = data.res.openid;
            var unionid = data.res.unionid;
            var pl = data.from;
            wx.setStorageSync('unionid', unionid);
            wx.setStorageSync('openid', opid);
            reslove(true);
          }
        })
      }
    });
  })
},
})